package com.soft.sitegenerator.business.hibernate;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.soft.sitegenerator.dao.HibernateDAO;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class User {

	private Long id;

	private String password;

	private String userName;

	private Date lastPostRead;

	private Profile profile;

	private Person person;

	private Set newsGroups = new HashSet();

	private Set myGroups = new HashSet();

	private Set publishedPost = new HashSet();

	private Set myWebSites = new HashSet();

	private Set templatesToComplete = new HashSet();

	private HibernateDAO hibernateDAOHib;

	public Date getLastPostRead() {
		return lastPostRead;
	}

	public void setLastPostRead(Date lastPostRead) {
		this.lastPostRead = lastPostRead;
	}

	public Set getMyGroups() {
		return myGroups;
	}

	public void setMyGroups(Set myGroups) {
		this.myGroups = myGroups;
	}

	public Set getNewsGroups() {
		return newsGroups;
	}

	public void setNewsGroups(Set newsGroups) {
		this.newsGroups = newsGroups;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Set getPublishedPost() {
		return publishedPost;
	}

	public void setPublishedPost(Set publishedPost) {
		this.publishedPost = publishedPost;
	}

	// HIBERNATE DAO
	public HibernateDAO getHibernateDAOHib() {
		return hibernateDAOHib;
	}

	public void setHibernateDAOHib(HibernateDAO hibernateDAOHib) {
		this.hibernateDAOHib = hibernateDAOHib;
	}

	public void load() throws ObjectNotFoundException {
		hibernateDAOHib.load(this, this.getId());
	}

	public void save() {
		hibernateDAOHib.save(this);
	}

	public void update() {
		hibernateDAOHib.update(this);
	}

	public void delete() {
		hibernateDAOHib.delete(this);
	}

	public boolean exist() {
		try {
			hibernateDAOHib.exist(this, this.getId());
			return true;
		} catch (ObjectNotFoundException e) {
			return false;
		}
	}

	public Collection findAll() {
		return hibernateDAOHib.find("from User order by userName");
	}

	public User findByUserName(String userName) {
		Collection users = hibernateDAOHib
				.find("from User where userName like '" + userName + "'");
		if (users.size() != 0)
			return (User) users.iterator().next();
		return null;
	}

	public Set getMyWebSites() {
		return myWebSites;
	}

	public void setMyWebSites(Set myWebSites) {
		this.myWebSites = myWebSites;
	}

	public Set getTemplatesToComplete() {
		return templatesToComplete;
	}

	public void setTemplatesToComplete(Set templatesToComplete) {
		this.templatesToComplete = templatesToComplete;
	}

}
